/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.util;

import hirondelle.date4j.DateTime;
import java.util.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import mail.Sender;

/**
 *
 * @author marcy
 */
public class beanRichiamo {

    private List<Object[]> patientList;
    private Map< Object[], Boolean> selectedListTmp;
    private List<Object[]> selectedList = null;

    public beanRichiamo() {
        selectedListTmp = new HashMap< Object[], Boolean>();
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("PortaleVisitePU");
        EntityManager em = emf.createEntityManager();
        patientList = em.createNativeQuery("Select P.nome, P.cognome, max(E.scadenza), count( E.effettuata ), R.email, R1.email "
                + "from Examination E, Referenze R, Referenze R1, Patient P "
                + "where E.id_pat = P.id "
                + "and E.effettuata = 0 "
                + "and R.id_paz = E.id_pat "
                + "and R.id_capovillaggio = R1.id_paz "
                + "and E.scadenza < now() "
                + "and E.scadenza > '"+DateTime.now(TimeZone.getDefault()).minusDays(365).format("YYYY-MM-DD hh:mm")+"' "
                + "Group by E.id_pat").getResultList();

    }

    public Map<Object[], Boolean> getSelectedListTmp() {
        return selectedListTmp;
    }

    public void setSelectedListTmp(Map<Object[], Boolean> selectedList) {
        this.selectedListTmp = selectedList;
    }

    public List getPatientList() {
        return patientList;
    }

    public void setPatientList(List patientList) {
        this.patientList = patientList;
    }

    public List<Object[]> getSelectedList() {
        return selectedList;
    }

    public void setSelectedList(List<Object[]> selectedList) {
        this.selectedList = selectedList;
    }

    public String richiamaAssenti() {
        setSelectedList(new ArrayList<Object[]>());
        for (Object[] ns : selectedListTmp.keySet()) {
            if (selectedListTmp.get(ns) == true) {
                selectedList.add(ns);
            }
        }
        Sender sender = new Sender();
        sender.sendNonPresentato(selectedList);
        setSelectedListTmp((new HashMap<Object[],Boolean>()));
        return "success";
    }
}
